<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!-- 输入框位置 -->
      <el-row :gutter="10" type="flex" justify="space-between">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            ref="input"
            @clear="getGoodsList()"
            @keydown.enter.native="queryChange"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" class="addUser">
          <el-button type="primary" class="addUserBtn" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格数据展示 -->
      <el-table :data="goodsList" border style="width: 100%" height="calc(100vh - 250px)">
        <el-table-column align="center" type="index" label="序号"></el-table-column>
        <el-table-column align="center" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column width="100" align="center" prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column width="100" align="center" prop="goods_number" label="商品数量"></el-table-column>
        <el-table-column width="100" align="center" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column align="center" label="创建时间">
          <template v-slot="scoped">
            <span>{{ scoped.row.add_time | timestampToTime}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="mg_state" label="操作" width="200px">
          <template v-slot="operation">
            <!-- 修改商品信息 -->
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(operation.row)"
            ></el-button>
            <!-- 删除商品 -->
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deleteGoods(operation.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      queryInfo: {
        //   查询参数
        query: "",
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      addGoodsDialogVisible: false,
      goodsList: [],
      total: 0
    };
  },
  created: function() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList: async function() {
      const { data: res } = await this.$http.get("/goods", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表数据失败了");
      }
      this.$message.success("获取列表数据成功");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    //改变当前显示多少条的时候触发
    handleSizeChange: function(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    // 改变当前的页码的时候触发
    handleCurrentChange: function(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    // 查询按回车的时候
    queryChange: function(event) {
      if (event.keyCode === 13) {
        // this.$refs.input.blur();
        this.getGoodsList();
      }
    },
    deleteGoods: function(_goodsInfo) {
      console.log(_goodsInfo);

      this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        lockScroll: false
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            "/goods/" + _goodsInfo.goods_id
          );
          if (res.meta.status !== 200) {
            this.$message({
              showClose: true,
              message: "删除失败",
              type: "error"
            });
            return;
          }
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success"
          });
          this.getGoodsList();
        })
        .catch(() => {});
    },
    goAddPage: function() {
      this.$router.push("/goods/add");
    }
  },
  filters: {
    timestampToTime: function(timestamp) {
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    }
  }
};
</script>
<style lang='less' scoped>
.el-pagination {
  margin-top: 10px;
}
.el-table {
  margin-top: 10px;
}
</style>